using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using Spacejock;
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using yW5Lib;

namespace yWriter6
{
	public class clsPrintWorkSchedule
	{
		public delegate void PrintProgressEventHandler(string ProgressString);

		[CompilerGenerated]
		private PrintProgressEventHandler PrintProgressEvent;

		public event PrintProgressEventHandler PrintProgress
		{
			[CompilerGenerated]
			add
			{
				PrintProgressEventHandler printProgressEventHandler = PrintProgressEvent;
				PrintProgressEventHandler printProgressEventHandler2;
				do
				{
					printProgressEventHandler2 = printProgressEventHandler;
					PrintProgressEventHandler value2 = (PrintProgressEventHandler)Delegate.Combine(printProgressEventHandler2, value);
					printProgressEventHandler = Interlocked.CompareExchange(ref PrintProgressEvent, value2, printProgressEventHandler2);
				}
				while ((object)printProgressEventHandler != printProgressEventHandler2);
			}
			[CompilerGenerated]
			remove
			{
				PrintProgressEventHandler printProgressEventHandler = PrintProgressEvent;
				PrintProgressEventHandler printProgressEventHandler2;
				do
				{
					printProgressEventHandler2 = printProgressEventHandler;
					PrintProgressEventHandler value2 = (PrintProgressEventHandler)Delegate.Remove(printProgressEventHandler2, value);
					printProgressEventHandler = Interlocked.CompareExchange(ref PrintProgressEvent, value2, printProgressEventHandler2);
				}
				while ((object)printProgressEventHandler != printProgressEventHandler2);
			}
		}

		public void PrintWorkSchedule(clsProject cProject, int OnlyThisStatus = 0)
		{
			//Discarded unreachable code: IL_0849, IL_0863, IL_0871, IL_0881, IL_0aab, IL_0aad, IL_0ac1, IL_0ae3
			int num = default(int);
			int num13 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				clsReport clsReport = new clsReport(ref clsShared.cLog, ref clsShared.cSettings, "Work Schedule");
				num2 = 3;
				int num3;
				int num4;
				if (OnlyThisStatus == 0)
				{
					num2 = 4;
					num3 = 2;
					num2 = 5;
					num4 = Information.UBound(modVariables.StatusNames);
				}
				else
				{
					num2 = 7;
					num3 = OnlyThisStatus + 1;
					num2 = 8;
					num4 = OnlyThisStatus + 1;
				}
				num2 = 9;
				clsShared.cSubs.BusyMouse();
				num2 = 10;
				clsReport.AddTitle();
				num2 = 11;
				clsReport.AddBR();
				num2 = 12;
				clsReport.OpenTable();
				num2 = 13;
				clsReport.OpenRow();
				num2 = 14;
				clsReport.OpenCell();
				num2 = 15;
				string str = "Project work schedule. ";
				num2 = 16;
				DateTime dateTime;
				if (DateAndTime.Hour(DateAndTime.Now) >= 22)
				{
					num2 = 17;
					dateTime = DateAndTime.Now.AddDays(1.0).Date;
					num2 = 18;
					str = str + " (Starting date set to " + Conversions.ToString(dateTime) + " because it's after 10pm)";
				}
				else
				{
					num2 = 20;
					dateTime = DateAndTime.Now.Date;
					num2 = 21;
					str = str + " (Starting date " + Conversions.ToString(dateTime) + ")";
				}
				num2 = 22;
				clsReport.AddCentered(str);
				num2 = 23;
				clsReport.AddCentered("Days left until project completion date: " + Conversions.ToString(cProject.get_DeadlineDate(5).Subtract(dateTime).Days));
				num2 = 24;
				clsReport.AddBR();
				num2 = 25;
				int[] array = new int[8];
				num2 = 26;
				int[] array2 = new int[8];
				num2 = 27;
				int[] array3 = new int[8];
				num2 = 28;
				IEnumerator enumerator = cProject.AllChaptersUsedNoInfoType().GetEnumerator();
				while (enumerator.MoveNext())
				{
					clsChapter clsChapter = (clsChapter)enumerator.Current;
					num2 = 29;
					int sceneCount = clsChapter.SceneCount;
					for (int i = 1; i <= sceneCount; i++)
					{
						num2 = 30;
						clsScene clsScene = cProject.get_SceneByID(clsChapter.get_SceneIDByIndex(i));
						num2 = 31;
						if (!clsScene.Unused)
						{
							num2 = 32;
							int num5 = clsScene.Status + 1;
							num2 = 33;
							array[num5]++;
						}
						num2 = 34;
					}
					num2 = 35;
				}
				num2 = 36;
				if (enumerator is IDisposable)
				{
					(enumerator as IDisposable).Dispose();
				}
				num2 = 37;
				int num6 = array.Length - 1;
				for (int i = 2; i <= num6; i++)
				{
					num2 = 38;
					array[i] += array[i - 1];
					num2 = 39;
				}
				num2 = 40;
				str = "To complete at each level: ";
				num2 = 41;
				int num7 = 1;
				do
				{
					num2 = 42;
					int num8 = NumberDaysLeft(cProject, num7, dateTime);
					num2 = 43;
					array3[num7] = 0;
					num2 = 44;
					if (array[num7] > 0)
					{
						num2 = 45;
						if (num8 > 0)
						{
							num2 = 46;
							array2[num7] = array[num7] / num8;
							num2 = 47;
							if (array[num7] % num8 > 0)
							{
								num2 = 48;
								array3[num7] = array[num7] - num8 * array2[num7];
							}
						}
						else
						{
							num2 = 50;
							array2[num7] = array[num7];
						}
						num2 = 51;
						if (array2[num7] == 0)
						{
							num2 = 52;
							array2[num7] = 1;
							num2 = 53;
							array3[num7] = 0;
						}
					}
					else
					{
						num2 = 55;
						array2[num7] = 0;
					}
					num2 = 56;
					if (num7 > 1)
					{
						num2 = 57;
						str = str + "  " + clsReport.FontColour(clsShared.cLang.GetTranslation(modVariables.StatusNames[num7]), modVariables.StatusColours[num7]) + " (" + Conversions.ToString(array[num7]) + ") ";
					}
					num2 = 58;
					num7++;
				}
				while (num7 <= 5);
				num2 = 59;
				clsReport.AddCentered(Strings.Trim(str));
				num2 = 60;
				clsReport.AddBR();
				num2 = 61;
				num2 = 62;
				clsReport.OpenTable();
				num2 = 63;
				int num9 = num3;
				int num10 = num4;
				bool flag3 = default(bool);
				for (int j = num9; j <= num10; j++)
				{
					num2 = 64;
					PrintProgressEvent?.Invoke(clsShared.cLang.GetTranslation("Printing") + " " + clsShared.cLang.GetTranslation(modVariables.StatusNames[j]));
					num2 = 65;
					bool flag = false;
					num2 = 66;
					int num11 = 0;
					num2 = 67;
					int num12 = 0;
					num2 = 68;
					bool flag2 = false;
					num2 = 69;
					DateTime printDate;
					if (DateTime.Compare(cProject.get_DeadlineStartDate(j), dateTime) < 0)
					{
						num2 = 70;
						printDate = dateTime;
					}
					else
					{
						num2 = 72;
						printDate = cProject.get_DeadlineStartDate(j);
					}
					num2 = 73;
					if (DateTime.Compare(cProject.get_DeadlineDate(j), dateTime) < 0)
					{
						num2 = 74;
					}
					else
					{
						num2 = 76;
						dateTime = cProject.get_DeadlineDate(j);
					}
					num2 = 77;
					IEnumerator enumerator2 = cProject.AllChaptersUsedNoInfoType().GetEnumerator();
					while (enumerator2.MoveNext())
					{
						clsChapter clsChapter = (clsChapter)enumerator2.Current;
						num2 = 78;
						int sceneCount2 = clsChapter.SceneCount;
						for (int i = 1; i <= sceneCount2; i++)
						{
							num2 = 79;
							clsScene clsScene = cProject.get_SceneByID(clsChapter.get_SceneIDByIndex(i));
							num2 = 80;
							clsCharacter clsCharacter = cProject.get_CharByID(clsScene.ViewPointID);
							num2 = 81;
							if (!clsScene.Unused)
							{
								num2 = 82;
								if (clsScene.Status <= j - 1)
								{
									num2 = 83;
									num11 += clsScene.Wordcount;
									num2 = 84;
									if (!flag)
									{
										num2 = 85;
										clsReport.OpenRow();
										num2 = 86;
										clsReport.AddCell(clsReport.Center(clsReport.H2("You need to turn the following scenes into '" + clsReport.FontColour(modVariables.StatusNames[j], modVariables.StatusColours[j]) + "' by " + Conversions.ToString(dateTime))), "colspan=2");
										num2 = 87;
										clsReport.CloseRow();
										num2 = 88;
										flag = true;
									}
									num2 = 89;
									clsReport.OpenRow();
									num2 = 90;
									clsReport.AddCell(clsChapter.Title + "." + Strings.Format(i));
									num2 = 91;
									clsReport.AddCell(clsScene.Title + " (" + Conversions.ToString(clsScene.Wordcount) + "w, " + clsCharacter.Title + ", <b>" + clsReport.FontColour(clsShared.cLang.GetTranslation(modVariables.StatusNames[clsScene.Status]), modVariables.StatusColours[clsScene.Status]) + "</b>)");
									num2 = 92;
									clsReport.CloseRow();
									num2 = 93;
									array[j]--;
									num2 = 94;
									num12++;
									num2 = 95;
									if (array[j] <= 0)
									{
										break;
									}
									num2 = 96;
									if (num12 >= array2[j])
									{
										num2 = 97;
										if (array3[j] > 0 && !flag2)
										{
											num2 = 98;
											flag2 = true;
											num2 = 99;
											array3[j]--;
										}
										else
										{
											num2 = 101;
											flag2 = false;
											num2 = 102;
											num12 = 0;
											num2 = 103;
											str = CreateStatusString(printDate.Date, num11);
											num2 = 104;
											clsReport.OpenRow();
											num2 = 105;
											clsReport.AddCell(str, "colspan=2, align='center'");
											num2 = 106;
											clsReport.CloseRow();
											num2 = 107;
											flag3 = false;
											num2 = 108;
											printDate = printDate.Date.AddDays(1.0);
											num2 = 109;
											num11 = 0;
										}
									}
									else
									{
										num2 = 111;
										flag2 = false;
									}
								}
							}
							num2 = 114;
						}
						num2 = 115;
					}
					num2 = 116;
					if (enumerator2 is IDisposable)
					{
						(enumerator2 as IDisposable).Dispose();
					}
					num2 = 117;
					if ((j > 0 && flag) & !flag3)
					{
						num2 = 118;
						str = CreateStatusString(printDate, num11);
						num2 = 119;
						clsReport.OpenRow();
						num2 = 120;
						clsReport.AddCell(str, "colspan=2, align='center'");
						num2 = 121;
						clsReport.CloseRow();
						num2 = 122;
						clsReport.AddBR();
					}
					num2 = 123;
				}
				num2 = 124;
				clsReport.CloseTable();
				num2 = 125;
				clsReport.CloseCell();
				num2 = 126;
				clsReport.CloseRow();
				num2 = 127;
				clsReport.CloseTable();
				num2 = 128;
				clsReport.AddFooter(App.Title);
				num2 = 129;
				clsReport.Show();
				num2 = 130;
				PrintProgressEvent?.Invoke("Printing Complete");
				num2 = 131;
				clsShared.cSubs.NormalMouse();
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num13 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_0ae1: Could not find block for branch target IL_0aad*/;
			}
			if (num13 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		private string CreateStatusString(DateTime PrintDate, int WordCount)
		{
			try
			{
				return "--- <font color=red>Complete by " + Strings.Format(PrintDate, "ddd") + " " + Conversions.ToString(PrintDate.Date) + "</font>" + new string('-', 30) + "(Current total " + Conversions.ToString(WordCount) + " words)" + new string('-', 10);
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				string result = "--- <font color=red>Complete by ERROR CALCULATING DATE</font>";
				ProjectData.ClearProjectError();
				return result;
			}
		}

		private int NumberDaysLeft(clsProject cProject, int Index, DateTime StartDate)
		{
			int num = 0;
			try
			{
				DateTime dateTime = cProject.get_DeadlineStartDate(Index);
				if (DateTime.Compare(dateTime, StartDate) < 0)
				{
					dateTime = StartDate;
				}
				num = cProject.get_DeadlineDate(Index).Subtract(dateTime).Days + 1;
				if (num >= 0)
				{
					return num;
				}
				num = 0;
				return num;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return num;
			}
		}
	}
}
